Skip to content

Implement unstable trait impl #140399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Implement unstable trait impl #140399

wants to merge 8 commits into from

Conversation

tiif
Copy link
Member

@tiif tiif commented Apr 28, 2025

This PR allows marking impls of stable trait with stable type as unstable.

Approach

In std/core, an impl can be marked as unstable by annotating it with #[unstable_feature_bound(feat_name)]. This will add a ClauseKind::Unstable_Feature(feat_name) to the list of predicates in predicates_of .

When an unstable impl's function is called, we will first iterate through all the goals in param_env to check if there is any ClauseKind::UnstableFeature(feat_name) in param_env.

The existence of ClauseKind::Unstable_Feature(feat_name) in param_env means an#[unstable_feature_bound(feat_name)] is present at the call site of the function, so we allow the check to succeed in this case.

If ClauseKind::UnstableFeature(feat_name) does not exist in param_env, we will still allow the check to succeed for either of the cases below:

  1. The feature is enabled through #[feature(feat_name)] outside of std / core.
  2. We are in codegen because we may be monomorphizing a body from an upstream crate which had an unstable feature enabled that the downstream crate do not.

For the rest of the case, it will fail with ambiguity.

Limitation

In this PR, we do not support:

  1. using items that need #[unstable_feature_bound] within stable APIs
  2. annotate main function with #[unstable_feature_bound]
  3. annotate #[unstable_feature_bound] on items other than free function and impl

Acknowledgement

The design and mentoring are done by @BoxyUwU

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Apr 28, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU self-assigned this May 27, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jun 10, 2025

☔ The latest upstream changes (presumably #142299) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot
Copy link
Collaborator

rustbot commented Jul 14, 2025

This PR changes Stable MIR

cc @oli-obk, @celinval, @ouz-a

@BoxyUwU
Copy link
Member

BoxyUwU commented Jul 14, 2025

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jul 14, 2025

⌛ Trying commit aa746cb with merge a3b3f37

To cancel the try build, run the command @bors2 try cancel.

rust-bors bot added a commit that referenced this pull request Jul 14, 2025
Implement unstable trait impl

This PR allows marking impls of stable trait with stable type as unstable.

## Approach

In std/core, an impl can be marked as unstable by annotating it with ``#[unstable_feature_bound(feat_name)]``. This will add a ``ClauseKind::Unstable_Feature(feat_name)`` to the list of predicates in ``predicates_of`` .

When an unstable impl's function is called, we will first iterate through all the goals in ``param_env`` to check if there is any ``ClauseKind::UnstableFeature(feat_name)`` in ``param_env``.

The existence of ``ClauseKind::Unstable_Feature(feat_name)`` in ``param_env`` means an``#[unstable_feature_bound(feat_name)]`` is present at the call site of the function, so we allow the check to succeed in this case.

If ``ClauseKind::UnstableFeature(feat_name)`` does not exist in ``param_env``, we will still allow the check to succeed for either of the cases below:
1. The feature is enabled through ``#[feature(feat_name)]`` outside of std / core.
2. We are in codegen because we may be monomorphizing a body from an upstream crate which had an unstable feature enabled that the downstream crate do not.

For the rest of the case, it will fail with ambiguity.

## Limitation

In this PR, we do not support:
1. using items that need ``#[unstable_feature_bound]`` within stable APIs
2. annotate main function with ``#[unstable_feature_bound]``
3. annotate ``#[unstable_feature_bound]`` on items other than free function and impl

## Acknowledgement
The design and mentoring are done by `@BoxyUwU`
@rust-log-analyzer

This comment has been minimized.

@lcnr
Copy link
Contributor

lcnr commented Jul 14, 2025

@bors2 try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Jul 14, 2025
Implement unstable trait impl

This PR allows marking impls of stable trait with stable type as unstable.

## Approach

In std/core, an impl can be marked as unstable by annotating it with ``#[unstable_feature_bound(feat_name)]``. This will add a ``ClauseKind::Unstable_Feature(feat_name)`` to the list of predicates in ``predicates_of`` .

When an unstable impl's function is called, we will first iterate through all the goals in ``param_env`` to check if there is any ``ClauseKind::UnstableFeature(feat_name)`` in ``param_env``.

The existence of ``ClauseKind::Unstable_Feature(feat_name)`` in ``param_env`` means an``#[unstable_feature_bound(feat_name)]`` is present at the call site of the function, so we allow the check to succeed in this case.

If ``ClauseKind::UnstableFeature(feat_name)`` does not exist in ``param_env``, we will still allow the check to succeed for either of the cases below:
1. The feature is enabled through ``#[feature(feat_name)]`` outside of std / core.
2. We are in codegen because we may be monomorphizing a body from an upstream crate which had an unstable feature enabled that the downstream crate do not.

For the rest of the case, it will fail with ambiguity.

## Limitation

In this PR, we do not support:
1. using items that need ``#[unstable_feature_bound]`` within stable APIs
2. annotate main function with ``#[unstable_feature_bound]``
3. annotate ``#[unstable_feature_bound]`` on items other than free function and impl

## Acknowledgement
The design and mentoring are done by `@BoxyUwU`
@rust-bors
Copy link

rust-bors bot commented Jul 14, 2025

⌛ Trying commit dc8f28f with merge 93970f9

(The previously running try build was automatically cancelled.)

To cancel the try build, run the command @bors2 try cancel.

@rust-log-analyzer

This comment has been minimized.

@tiif
Copy link
Member Author

tiif commented Jul 14, 2025

I should have made the CI green first... will request for perf run afterward.

@rust-bors
Copy link

rust-bors bot commented Jul 14, 2025

☀️ Try build successful (CI)
Build commit: 93970f9 (93970f9d9c3e81fa0797ad083455b0a1f5e54c54, parent: 64b185eddaa1d7ddf5e0c024be23b9cbba6c1722)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (93970f9): comparison URL.

Overall result: ❌ regressions - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.1%, 0.8%] 62
Regressions ❌
(secondary)
0.4% [0.1%, 1.3%] 61
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 1
All ❌✅ (primary) 0.3% [0.1%, 0.8%] 62

Max RSS (memory usage)

Results (primary -4.9%, secondary 2.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.8% [2.4%, 3.1%] 2
Improvements ✅
(primary)
-4.9% [-4.9%, -4.9%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -4.9% [-4.9%, -4.9%] 1

Cycles

Results (primary 2.2%, secondary 3.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.2% [2.2%, 2.2%] 2
Regressions ❌
(secondary)
3.0% [3.0%, 3.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.2% [2.2%, 2.2%] 2

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 463.69s -> 465.733s (0.44%)
Artifact size: 374.63 MiB -> 374.51 MiB (-0.03%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jul 14, 2025
@tiif
Copy link
Member Author

tiif commented Jul 14, 2025

The only difference between the latest commit and the one being used for perf is comment.

Hi @compiler-errors, I think you might be interested to see the perf result.

@bors
Copy link
Collaborator

bors commented Jul 14, 2025

☔ The latest upstream changes (presumably #143934) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants